/**  
 * @Title:  CheckCodeController.java   
 * @Package com.platform.controller   
 * @Description: (用一句话描述该文件做什么)   
 * @author: <a href="mailto:15909910367@163.com">吴先生</a> 
 * @date:   2020年8月31日 下午2:08:33   
 * @version V1.0 
 * @Copyright: 2020 吴先生.
 */
package com.platform.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import javax.imageio.ImageIO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.platform.base.R;
import com.platform.base.mvc.BaseController;
import com.platform.ext.cache.utils.RedisCache;

import cn.hutool.core.codec.Base64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**   
 * @ClassName:  CheckCodeController   
 * @Description:校验码生成
 * @author: <a href="mailto:15909910367@163.com">吴先生</a>  
 * @date:   2020年8月31日 下午2:08:33   
 *     
 * @Copyright: 2020 吴元森.  
 */
@Controller
@Api(tags="COMMON-校验码服务")
@RequestMapping("common")
public class CheckCodeController extends BaseController{
	
	@Autowired
	private RedisCache redisCache;
	
	@RequestMapping(value="/getCaptchaCode",method = RequestMethod.GET)
	@ApiOperation(value = "生成校验码")
	@ResponseBody
	public R<?> captchaCode() throws Exception{
		//0.创建空白图片
		BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);
		//1.获取图片画笔
		Graphics g = image.getGraphics();
		Random r = new Random();
		//2.设置画笔颜色
		g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
		//3.绘制矩形的背景
		g.fillRect(0, 0, 100, 30);	
		//4.调用自定义的方法，获取长度为5的字母数字组合的字符串
		String number = getNumber(5);	
		//将图片字符存入session,用于验证码检查使用
		g.setColor(new Color(0,0,0));
		g.setFont(new Font(null,Font.BOLD,24));
		//5.设置颜色字体后，绘制字符串
		g.drawString(number, 5, 25);
		//6.绘制8条干扰线
		for(int i=0;i<8;i++){
			g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),r.nextInt(255)));
			g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));
		}
		//将图片转换陈字符串给前端
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpeg", stream);
        String base64 = Base64.encode(stream.toByteArray());
        stream.flush();
        stream.close();
        String uuid = getRemortIP(request).replace(":", ".");
        redisCache.setCacheObject("platform:captchaCode:captcha_"+uuid, number,60,TimeUnit.SECONDS);
        return R.success("data:image/jpeg;base64,"+base64).setExtra(uuid);
	}
	
	private String getNumber(int size){
		String str = "qwertyuioplkjhgfdsazxcvbnmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		String number = "";
		Random r = new Random();
		for(int i=0;i<size;i++){
			number+=str.charAt(r.nextInt(str.length()));
		}
		return number;
	}
}
